package com.jiangnan.ui.activity

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.chad.library.adapter4.BaseQuickAdapter
import com.jiangnan.R
import com.jiangnan.databinding.ActivityBookCollectedBinding
import com.jiangnan.ui.adapter.CollectBookQuickAdapter
import com.jiangnan.ui.adapter.ReturnBookQuickAdapter
import com.jiangnan.util.SharedPrefUtil
import com.jiangnan.util.ToastUtil
import com.jiangnan.viewmodel.BookViewModel
import com.jiangnan.viewmodel.InteractionViewModel
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout

class BookCollectedActivity : AppCompatActivity() {

    private lateinit var binding: ActivityBookCollectedBinding
    private var adapter: CollectBookQuickAdapter? = null
    private lateinit var interactionViewModel: InteractionViewModel
    private var currentUserId: Int? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = DataBindingUtil.setContentView(this, R.layout.activity_book_collected)
        binding.lifecycleOwner = this
        interactionViewModel = ViewModelProvider(this)[InteractionViewModel::class.java]

        currentUserId = SharedPrefUtil.getLoginInfo(this)!!.id


        // 初始化监听器
        initObserver()
        initRefreshLayout()
        initAdapter()
        toastMessage()


    }

    private fun initAdapter() {
        var recyclerView = binding.recyclerView
        recyclerView.layoutManager = StaggeredGridLayoutManager(
            1, StaggeredGridLayoutManager.VERTICAL
        )
        adapter = CollectBookQuickAdapter()
        adapter!!.setItemAnimation(BaseQuickAdapter.AnimationType.AlphaIn)
        recyclerView.adapter = adapter

        // 需要传递控件 id
        adapter!!.addOnItemChildClickListener(R.id.btn_return) { _, view, position ->
            interactionViewModel.cancelFavorite(currentUserId!!.toInt(), adapter!!.getItem(position)!!.id!!)
        }
    }

    /**
     * 初始化下拉刷新和上拉加载
     */
    private fun initRefreshLayout() {
        val refreshLayout = binding.refreshLayout as RefreshLayout
        refreshLayout.setRefreshHeader(ClassicsHeader(this))
//        refreshLayout.setRefreshFooter(ClassicsFooter(this))
        refreshLayout.setOnRefreshListener { refreshlayout ->
            interactionViewModel.getFavoriteBooks(currentUserId!!.toInt())
            refreshlayout.finishRefresh(true) //传入false表示刷新失败
        }
//        refreshLayout.setOnLoadMoreListener { refreshlayout ->
//            refreshlayout.finishLoadMore(200 /*,false*/) //传入false表示加载失败
//        }
    }

    /**
     * 所有的监听器在这里初始化
     */
    private fun initObserver() {
        interactionViewModel.getFavoriteBooks(currentUserId!!.toInt())

        /**
         * 监听返回结果
         */

        interactionViewModel.favoriteBooks.observe(this) {
            binding.tvTotal.text = "您收藏的书籍共有 ${it.size} 本"
            adapter!!.submitList(it)
        }


    }

    private fun toastMessage() {
        interactionViewModel.toastMessage.observe(this) { message ->
            ToastUtil.showShort(this, message)
        }
    }
}